package cfca.seal.web.service;

import cfca.seal.bean.Organization;
import cfca.seal.common.Page;
import cfca.seal.dao.main.IOrganizationDao;
import cfca.seal.exception.CodeException;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class OrganizationService
{

  @Resource
  private IOrganizationDao organizationDao;

  public Organization getOrganizationById(String id)
    throws Exception
  {
    Organization organization = this.organizationDao.getOrganizationById(id);
    return organization;
  }

  public Page<Organization> getOrganizations(String pageNo) throws Exception {
    Page page = this.organizationDao.getPage(pageNo);
    return page;
  }

  public boolean deleteById(String id) throws Exception {
    return this.organizationDao.deleteById(id);
  }

  public boolean update(Organization organization) throws Exception {
    Organization organizationEntity = this.organizationDao.getOrganizationByInfoForUpdate(organization.getCode(), organization.getName(), organization.getIdentificationType(), organization.getIdentificationNo());

    if (organizationEntity != null)
      throw new CodeException("C7002", "相同机构编码或相同三要素(机构名称、机构用户证件类型、机构用户证件号)的企业机构已经存在");
    return this.organizationDao.update(organization);
  }

  public boolean addOrganization(Organization organization) throws Exception {
    Organization organizationEntity = this.organizationDao.getOrganizationByInfoForInsert(organization.getCode(), organization.getName(), organization.getIdentificationType(), organization.getIdentificationNo());

    if (organizationEntity != null)
      throw new CodeException("C7002", "相同机构编码或相同三要素(机构名称、机构用户证件类型、机构用户证件号)的企业机构已经存在");
    return this.organizationDao.insert(organization);
  }

  public int countAdminUserRecord(String organizationId) throws Exception {
    return this.organizationDao.countAdminUserRecord(organizationId);
  }

  public int countUsersRecord(String organizationId) throws Exception
  {
    return this.organizationDao.countUsersRecord(organizationId);
  }

  public int countSealImageRecord(String organizationId) throws Exception {
    return this.organizationDao.countSealImageRecord(organizationId);
  }

  public int countSealRecord(String organizationId) throws Exception {
    return this.organizationDao.countSealRecord(organizationId);
  }

  public int countSealCertRecord(String organizationId) throws Exception {
    return this.organizationDao.countSealCertRecord(organizationId);
  }

  public int countChildRecordByParenId(String deleteid) throws Exception {
    return this.organizationDao.countChildRecordByParenId(deleteid);
  }
}